package com.bosssoft.trainee.nontax.controller;


import com.alibaba.fastjson.JSON;
import com.bosssoft.trainee.nontax.entity.dto.PermissionDTO;
import com.bosssoft.trainee.nontax.entity.po.PermissionPO;
import com.bosssoft.trainee.nontax.service.PermissionService;
import com.bosssoft.trainee.nontax.utils.Result;
import com.bosssoft.trainee.nontax.entity.vo.RolePermissionVO;
import com.bosssoft.trainee.nontax.entity.vo.query.PermissionQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sct
 * @since 2022-06-15
 */
@RestController
@RequestMapping("/api/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    /**
     * 查询菜单列表
     */
    @GetMapping("/list")
    public Result<Object> getMenuList(PermissionQueryVO permissionQueryVO){
        PermissionDTO permissionDTO = new PermissionDTO();
        BeanUtils.copyProperties(permissionQueryVO,permissionDTO);
        List<PermissionDTO> permissionDTOList =permissionService.findPermissionList(permissionDTO);
        List<PermissionQueryVO> permissionQueryVOList;
        permissionQueryVOList = JSON.parseArray(JSON.toJSONString(permissionDTOList),PermissionQueryVO.class);
        return Result.ok(permissionQueryVOList);
    }

    /**
     * 查询上级菜单列表
     */
    @GetMapping("/parent/list")
    public Result<Object> getParentList(){
        List<PermissionDTO> permissionDTOList =permissionService.findParentPermissionList();
        List<PermissionQueryVO> permissionQueryVOList;
        permissionQueryVOList = JSON.parseArray(JSON.toJSONString(permissionDTOList),PermissionQueryVO.class);
        return Result.ok(permissionQueryVOList);
    }

    /**
     * 根据id查询菜单信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Object> getMenuById(@PathVariable Long id){
        return Result.ok(permissionService.getById(id));
    }

    /**
     * 添加菜单
     * @param permissionQueryVO
     * @return
     */
    @PostMapping("/add")
    public Result<Object> add(@RequestBody PermissionQueryVO permissionQueryVO){

        PermissionPO permissionPO = new PermissionPO();
        BeanUtils.copyProperties(permissionQueryVO,permissionPO);
        if (permissionService.save(permissionPO)){
            return Result.ok().message("菜单添加成功");
        }
        return Result.error().message("菜单添加失败");
    }

    /**
     * 修改菜单
     * @param permissionQueryVO
     * @return
     */
    @PutMapping("/update")
    public Result<Object> update(@RequestBody PermissionQueryVO permissionQueryVO){

        PermissionPO permissionPO = new PermissionPO();
        BeanUtils.copyProperties(permissionQueryVO,permissionPO);
        if (permissionService.updateById(permissionPO)){
            return Result.ok().message("菜单修改成功");
        }
        return Result.error().message("菜单修改失败");
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable Long id){
        if(permissionService.removeById(id)){
            return Result.ok().message("菜单删除成功");
        }
        return Result.error().message("菜单删除失败");
    }

    /**
     * 检查菜单下是否有子菜单
     * @param id
     * @return
     */
    @GetMapping("/check/{id}")
    public Result<Object> check(@PathVariable Long id){
        if(permissionService.hasChildrenOfPermission(id)){
            return Result.exist().message("该菜单下有子菜单，无法删除");
        }
        return Result.ok();
    }

    @GetMapping("/getAssignPermissionTree")
    public Result<Object> getAssignPermissionTree(Long userId, Long roleId) {
        RolePermissionVO permissionTree =permissionService.findPermissionTree(userId,roleId);
        return Result.ok(permissionTree);
    }
}

